package com.limingru.newbee.service.impl;

import com.limingru.newbee.api.param.CartModifyCountParam;
import com.limingru.newbee.api.param.CartSaveGoodsParam;
import com.limingru.newbee.api.vo.CartItemVO;
import com.limingru.newbee.common.NewBeeMallException;
import com.limingru.newbee.common.ServiceResultEnum;
import com.limingru.newbee.config.annotation.TokenToUser;
import com.limingru.newbee.dao.CartMapper;
import com.limingru.newbee.dao.GoodsMapper;
import com.limingru.newbee.entity.CartItem;
import com.limingru.newbee.entity.Goods;
import com.limingru.newbee.service.CartService;
import com.limingru.newbee.util.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public String saveGoodsInCart(CartSaveGoodsParam cartSaveGoodsParam, Long userId) {
        CartItem cartItem = cartMapper.selectGoodsByUserIdAndGoodsId(cartSaveGoodsParam.getGoodsId(), userId);
        if (cartItem != null) {
            NewBeeMallException.fail(ServiceResultEnum.GOODS_EXISTS_IN_CART.getText());
        }
        CartItem newCartItem = new CartItem();
        Date now = new Date();
        BeanUtils.copyProperties(cartSaveGoodsParam, newCartItem);
        newCartItem.setUserId(userId);
        newCartItem.setIsDeleted((byte)0);
        newCartItem.setCreateTime(now);
        newCartItem.setUpdateTime(now);
        int result = cartMapper.saveGoodsToCart(newCartItem);
        if (result > 0) {
            return ServiceResultEnum.SUCCESS.getText();
        }
        return ServiceResultEnum.FAIL.getText();
    }

    @Override
    public List<CartItemVO> getCartList(Long userId) {
        List<CartItem> cartItems = cartMapper.selectCartItemsByUserId(userId);
        List<Long> goodsIds = cartItems.stream().map(CartItem::getGoodsId).collect(Collectors.toList());
        List<CartItemVO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(goodsIds)) {
            return result;
        }
        List<Goods> goods = goodsMapper.selectGoodsByGoodsIds(goodsIds);

        if (!CollectionUtils.isEmpty(goods)) {
            Map<Long, Goods> collect = goods.stream().collect(Collectors.toMap(Goods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            for (CartItem c : cartItems) {
                CartItemVO cartItemVO = new CartItemVO();
                BeanUtils.copyProperties(c, cartItemVO);
                if (collect.containsKey(c.getGoodsId())) {
                    BeanUtils.copyProperties(collect.get(c.getGoodsId()), cartItemVO);
                }
                result.add(cartItemVO);
            }
        }
        return result;
    }

    @Override
    public String modifyCount(CartModifyCountParam cartModifyCountParam, Long userId) {
        CartItem cartItem = new CartItem();
        cartItem.setUserId(userId);
        cartItem.setCartItemId(cartModifyCountParam.getCartItemId());
        cartItem.setGoodsCount(cartModifyCountParam.getGoodsCount());
        int result = cartMapper.updateCartItemCount(cartItem);
        if (result > 0) {
            return ServiceResultEnum.SUCCESS.getText();
        }
        return ServiceResultEnum.FAIL.getText();
    }

    @Override
    public CartItem findCartItemById(Long cartItemId) {
        return cartMapper.findCartItemByCartItemId(cartItemId);
    }

    @Override
    public String removeCartItem(CartItem cartItem) {
        int removeResult = cartMapper.removeCartItem(cartItem);
        if (removeResult > 0) {
            return ServiceResultEnum.SUCCESS.getText();
        }
        return ServiceResultEnum.FAIL.getText();
    }

    @Override
    public List<CartItemVO> getCartItemsForSettle(List<Long> cartItemIds, Long userId) {
        List<CartItem> cartItems = cartMapper.getCartItemsForSettle(cartItemIds, userId);
        if (CollectionUtils.isEmpty(cartItems)) {
            NewBeeMallException.fail("购物项不能为空");
        }

        List<CartItemVO> result = new ArrayList<>();
        List<Long> goodsIdList = cartItems.stream().map(CartItem::getGoodsId).collect(Collectors.toList());
        List<Goods> goods = goodsMapper.selectGoodsByGoodsIds(goodsIdList);
        Map<Long, Goods> goodsMap = goods.stream().collect(Collectors.toMap(Goods::getGoodsId, Function.identity(), (a, b) -> a));

        for (CartItem c : cartItems) {
            CartItemVO cartItemVO = new CartItemVO();
            BeanUtils.copyProperties(c, cartItemVO);
            Long goodsId = c.getGoodsId();
            if (goodsMap.containsKey(goodsId)) {
                Goods currentGoods = goodsMap.get(goodsId);
                cartItemVO.setGoodsName(currentGoods.getGoodsName());
                cartItemVO.setGoodsCoverImg(currentGoods.getGoodsCoverImg());
                cartItemVO.setSellingPrice(currentGoods.getSellingPrice());
            }
            result.add(cartItemVO);
        }
        return result;
    }
}
